package com.itstu.gulimall.product.service.impl;

import com.itstu.gulimall.product.vo.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itstu.common.utils.PageUtils;
import com.itstu.common.utils.Query;

import com.itstu.gulimall.product.dao.SkuInfoDao;
import com.itstu.gulimall.product.entity.SkuInfoEntity;
import com.itstu.gulimall.product.service.SkuInfoService;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    ThreadPoolExecutor executor;

    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo vo = new SkuItemVo();
        CompletableFuture<SkuItemVo> info = CompletableFuture.supplyAsync(() -> {
            vo.setName("小明");
            if (skuId<60){
                vo.setAge(Math.toIntExact(skuId));
            }
            vo.setAddr("杭州");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第1个任务,设置"+"线程名字"+Thread.currentThread().getName());

            return vo;
        }, executor);
        CompletableFuture<Void> future = info.thenAcceptAsync((res) -> {
            Integer age = res.getAge();
            System.out.println("第2个任务  "+age+"线程名字"+Thread.currentThread().getName());
        }, executor);
        CompletableFuture<Void> future1 = info.thenAcceptAsync((res) -> {
            String name = res.getName();
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("第3个任务  "+name+"线程名字"+Thread.currentThread().getName());
        }, executor);

        CompletableFuture<Void> future2 = info.thenAcceptAsync((res) -> {
            String addr = res.getAddr();
            System.out.println("第4个任务  "+addr+"线程名字"+Thread.currentThread().getName());
        }, executor);

        CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> {
            System.out.println("第5个任务，看看能不能第一个执行"+"线程名字"+Thread.currentThread().getName());
        }, executor);

        try {
            CompletableFuture.allOf(info,future,future1,future2,future3).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return vo;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            queryWrapper.and((w)->{
               w.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        /**
         catelogId: 0,
         brandId: 0,
         min: 0,
         max: 0
         */
        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId)){
            queryWrapper.eq("catalog_id ",catelogId);
        }
        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId)){
            queryWrapper.eq("brand_id",brandId);

        }
        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(min)){
            // TODO 此处min是string类型，进行price价格对比吗？
            queryWrapper.ge("price",min);
        }
        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max)){
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if ( bigDecimal.compareTo(new BigDecimal("0")) == 1){
                    queryWrapper.le("price",max);
                }
            }catch (Exception e){

            }


        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);

    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId);
        List<SkuInfoEntity> list = this.list(wrapper);
        return list;
    }


}